package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.Invest;
import com.gxa.p2p.pojo.LoanBidsDetail;
import com.gxa.p2p.service.InvestService;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 投标记录表(Invest)表控制层
 */
@RestController
@CrossOrigin
@RequestMapping("invest")
@Api(value = "投资(投标)控制器", tags = "投资模块")
public class InvestController {

    @Resource
    private InvestService investService;

    /**
     * 查询所有数据 可带查询条件 且带分页条件
     * @param paramMap
     * @return Map
     */
    @PostMapping
    @ApiOperation(value = "查询所有数据的接口,可输入其他查询条件", response = Map.class, httpMethod = "POST")
    public Map<String,Object> findAll(@RequestBody @ApiParam(value = "参数map,包含姓名,电话,分页参数", required = true, type = "Map") Map<String,Object> paramMap) {
        MapUtil mapUtil = new MapUtil();
        List<LoanBidsDetail> list = investService.findAll(paramMap);
        List<LoanBidsDetail> list2 = investService.findAll2(paramMap);
        Integer dataSum = investService.dataSum(paramMap);
        if (dataSum > 0) {
            mapUtil.setCode(0);
            mapUtil.put("data",list);
            mapUtil.put("data2",list2);
            mapUtil.put("count",dataSum);
            return mapUtil.getMap();
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }

    /**
     * 新增数据
     * @param invest 实例对象
     * @return Map
     */
    @PostMapping("/insert")
    @ApiOperation(value = "添加投标的接口", response = Map.class, httpMethod = "POST")
    public Map<String,Object> insert(@RequestBody @ApiParam(value = "参数为invest对象,包含实体类中所有属性", required = true, type = "Invest") Invest invest) {
        MapUtil mapUtil = new MapUtil();
        Integer lines = investService.insert(invest);
        if (lines > 0) {
            mapUtil.setCode(0);
            return mapUtil.getMap();
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }

    /**
     * 修改数据
     * @param invest 实例对象
     * @return Map
     */
    @PutMapping
    @ApiOperation(value = "更新投标的接口", response = Map.class, httpMethod = "PUT")
    public Map<String,Object> update(@RequestBody @ApiParam(value = "参数为invest对象,包含实体类所需修改的属性", required = true, type = "Invest") Invest invest) {
        MapUtil mapUtil = new MapUtil();
        Integer lines = investService.update(invest);
        if (lines > 0) {
            mapUtil.setCode(0);
            return mapUtil.getMap();
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }

    /**
     * 通过主键删除数据
     * @param invest 实例对象
     * @return Map
     */
    @DeleteMapping
    @ApiOperation(value = "删除投标记录的接口", response = Map.class, httpMethod = "DELETE")
    @ApiImplicitParam(value = "参数为Integer型id",required = true,paramType = "Integer")
    public Map<String,Object> delete(@RequestParam Integer id) {
        MapUtil mapUtil = new MapUtil();
        Integer lines = investService.deleteById(id);
        if (lines > 0) {
            mapUtil.setCode(0);
            return mapUtil.getMap();
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }
}